﻿define('ui/controller/tradeLevelController',
	['local/cache', 'api/tradeService',
	 'ui/controller/conditionController', 'util/stringformat'], 
	function(cache, tradeService, conditionController, stringformat) {

	//console.log(window.subString);
	
	/**
	 * 地行业层级
	 */
	var tradeLevelController = {
		
		/**
		 * 初始化
		 * 1、条件栏地区默认选中全部行业
		 * 2、渲染所有行业：只到二级行业
		 */
		initial: function(){
			$("em.cl-trade>span").first().text("全部行业");
			
			//防止多次请求和多次渲染，这个地方应该是仅加载一次、渲染一次
			if(cache.getTradeLevelData() == null) {
				cache.setTradeLevelData(tradeService.getTrades());
				tradeLevelController.render(cache.getTradeLevelData());
			}
		},	
		
		/**
		 * 渲染全部行业：1类行业为第一层、2类行业为第二层
		 */
		render: function(data) {
			
			AMapUI.loadUI(['misc/MarkerList'], function(MarkerList) {
				//area-level1-select
				var province = 	 '<li id="cl-9999" class="area-level1-select">'+
									'<span class="cl-area-name">'+
									'全部行业' +
									'</span>'+
									'<span class="cl-right-arrow">'+
									'</span>'+
								 '</li>';
				var tradeLevel1List = '<% for(var i=0;i < data.length;i++) { %>'+ 
										 '<% if(i==0) { %>' +
										 '<li id="cl-<%- i %>" class="area-level1-select" title="<%- data[i].tradeName%>">'+
											'<span class="cl-area-name">'+
											'<%- subString(data[i].tradeName, 5) %>' +
											'</span>'+
											'<span class="cl-right-arrow">'+
											'</span>'+
										 '</li>'+
										 '<% } %>'+
										 '<% if(i>0) { %>' +
										 '<li id="cl-<%- i %>" class="" title="<%- data[i].tradeName%>">'+
											'<span class="cl-area-name">'+
											'<%- subString(data[i].tradeName, 5) %>' +
											'</span>'+
											'<span class="cl-right-arrow">'+
											'</span>'+
										 '</li>'+
										 '<% } %>'+
									 '<% } %>';
				//area-level2-select
				var province2List =  '<ul class="classify-area-level2" cl_num="9999" style="flex: 2 1 0%;">'+
										 '<li data-val="business_area_flag=1;adcode=;custom=business_area:全部行业">'+
											'全部'+
										 '</li>'+
									 '</ul>';
				var tradeLevel2List = '<% for(var i=0;i < data.length;i++) { %>'+ 
									  '<ul class="classify-area-level2" cl_num="<%- i %>" style="flex: 2 1 0%;">'+
										 '<li data-val="business_area_flag=1;codeTrade=<%- data[i].codeTrade %>;custom=business_area:<%- subString(data[i].tradeName, 4) %>">'+
											''+'全部'+
										 '</li>'+
										 '<% for(var j=0;j < data[i].children.length;j++) { %>'+ 
										 '<li title="<%- data[i].children[j].tradeName %>" data-val="business_area_flag=1;adcode=<%- data[i].children[j].codeTrade %>;custom=business_area:<%- data[i].children[j].tradeName %>">'+
											''+'<%- subString(data[i].children[j].tradeName, 3) %>'+
										 '</li>'+
										 '<% } %>'+
									  '</ul>'+
									  '<% } %>';
				var html = 	'<ul class="classify-area-level1">' + 
								province+
								MarkerList.utils.template(tradeLevel1List, {data: data}) + 
							'</ul>';
				html += (province2List+MarkerList.utils.template(tradeLevel2List, {data: data}));
				
				$("#trade").html(html); 
				
				tradeLevelController.onLevel1Select();
				tradeLevelController.onLevel2Select();
				
				//解决行业第二层级的高度样式问题
				tradeLevelController.selectLevel1First();
			});
		},
		
		/**
		 * 选中第一层级第一个元素
		 */
		selectLevel1First: function() {
			$('#trade ul.classify-area-level1 li').first().mouseover();
		},
		
		/**
		 * 选中第一层级：改变1类行业的样式选中，第二层显示该行业的2类行业
		 */
		onLevel1Select: function(item) {
			$('#trade ul.classify-area-level1 li').each(function(){
				$(this).mouseover(function(){
					$('#trade ul.classify-area-level1 li').removeClass('area-level1-select');
					$(this).addClass('area-level1-select');
					var c = $(this).attr('id');
					if(c && c.split('-').length==2) {
						var id = c.split('-')[1];
						$('#trade .classify-area-level2').css({"display":"none"});
						$('#trade .classify-area-level2').each(function(){
							if($(this).attr('cl_num')==id) {
								$(this).css({"display":"block"});
							}
						});
					}
				});
			});
		},

		/**
		 * 选中第二层级：改变2类行业的样式选中，条件栏行业的值显示改行业，并且调用企业MarkerList渲染地图
		 */
		onLevel2Select: function(item) {
			$('#trade ul.classify-area-level2 li').each(function(){
				$(this).click(function(){
					var val = $(this).attr('data-val');
					var s = val.split(";");
					var $span = $("em.cl-trade>span").first();
					if($(this).text()=='全部') {
						if(s && s.length>0){
							$span.text(s[2].split(":")[1]);
							conditionController.setTradeCode($span, s[1].split("=")[1]?s[1].split("=")[1]:'');
						}
					}else {
						$span.text($(this).text());
						conditionController.setTradeCode($span, s[1].split("=")[1]);
					}
					$('#trade ul.classify-area-level2 li').removeClass("area-level2-select");
					$(this).addClass("area-level2-select");
					conditionController.hideConditionList();
					
					//todo：enterprise
					var enterpriseML = require('ui/markerList/enterpriseML');
					enterpriseML.reRenderData();
				});
			});
		}
	};
	
	return tradeLevelController;
});